package com.tee.web.action.logon;


import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.tee.bean.privilege.User;
import com.tee.service.privilege.UserService;
import com.tee.utils.SiteUrl;
import com.tee.web.action.BaseAction;

@Controller
public class LogonAction extends BaseAction implements ModelDriven<User> {

	private static final long serialVersionUID = 8804344141599704148L;
	private User user = new User();
	@Resource(name = "userServiceBean")
	UserService userService;

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {

		// 如果没有空指针
		if (user.getPhone() != null && !"".equals(user.getPhone().trim())
				&& user.getPassword() != null && !"".equals(user.getPassword())) {

			// 如果用户名密码正确
			if (userService.checkUser(user.getPhone(), user.getPassword())) {
				user = userService.findByPhone(user.getPhone().trim());

				// 如果用户被禁用
				if (user.getVisible() == false) {
					request.setAttribute("message", "对不起您的账号已被禁用，无法登陆系统！");
					return INPUT;
				}
				if (session.get("user") != null) {
					session.remove("user");
				}
				session.put("user", user);
				return SUCCESS;
				
			} else { // 用户名密码错误
				request.setAttribute("message", "用户名或密码错误！");
				request.setAttribute("urladdress", SiteUrl.readUrl("logon"));
				return "message";
			}
		}
		return INPUT;
	}

	public User getModel() {
		return user;
	}

}
